package com.leyou.item.controller;


import com.leyou.common.pojo.PageResult;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.entity.Sku;
import com.leyou.item.entity.Spu;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;


    /**
     * 商品的分页查询
     * @param key  搜索关键字
     * @param saleable 是否上下架
     * @param page 第几页
     * @param rows 每页大小
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuDTO>> goodsPageQuery(@RequestParam(value = "key" , required = false) String key,
                                                             @RequestParam(value = "saleable" , required = false) Boolean saleable,
                                                             @RequestParam(value = "page" , defaultValue = "1") Integer page,
                                                             @RequestParam(value = "rows" , defaultValue = "5") Integer rows){

        PageResult<SpuDTO> pageResult = goodsService.goodsPageQuery(key , saleable , page , rows);
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 商品保存
     */
    @PostMapping("/goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDto){

        goodsService.saveGoods(spuDto);
        return ResponseEntity.status(HttpStatus.CREATED).build();

    }


    /**
     * 商品上下架
     */
    @PutMapping("/spu/saleable")
    public ResponseEntity<Void> updateSaleable(@RequestParam("id") Long id,
                                               @RequestParam("saleable") Boolean saleable){

        goodsService.updateSaleable(id , saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }


    /**
     * 根据SpuId查询SpuDetail对象
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetail> findSpuDetailById(@RequestParam("id") Long id){
        SpuDetail spuDetail = goodsService.findSpuDetailById(id);
        return ResponseEntity.ok(spuDetail);
    }

    /**
     * 根据SpuId查询Sku集合
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<Sku>> findSkusBySpuId(@RequestParam("id") Long id){
        List<Sku> skuList = goodsService.findSkusBySpuId(id);
        return ResponseEntity.ok(skuList);
    }

    /**
     * 根据spuId查询SpuDto对象
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> findSpuDtoById(@RequestParam("id") Long id){

        SpuDTO spuDTO = goodsService.findSpuDtoById(id);
        return ResponseEntity.ok(spuDTO);

    }


    /**
     * 根据id批量查询sku
     * @param ids skuId的集合
     * @return sku的集合
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<Sku>> findSkusByIds(@RequestParam("ids") List<Long> ids){

        List<Sku> skus = goodsService.findSkusByIds(ids);
        return ResponseEntity.ok(skus);
    }


    /**
     * 减库存
     * @param paramMap 商品id及数量的map
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> minusStock(@RequestBody Map<Long, Integer> paramMap){
        goodsService.minusStock(paramMap);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 归还失效订单的库存
     * @param skuId
     * @param num
     */
    @PutMapping("/sku/return")
    public ResponseEntity<Void> returnSku(Long skuId, Integer num){
        goodsService.returnSku(skuId, num);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


}
